package com.supremex.aisupremex.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import com.supremex.aisupremex.common.utils.TreeBuildUtils;
import com.supremex.aisupremex.web.dao.MenuMapper;
import com.supremex.aisupremex.web.entity.Menu;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supremex.aisupremex.web.entity.vo.MenuVo;
import com.supremex.aisupremex.web.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author zkx
 * @since 2024-06-05
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

	@Override
	public List<MenuVo> selectMenuList() {
		List<MenuVo> menuList = new ArrayList<>();
		List<Menu> list = this.list();
		if (CollUtil.isNotEmpty(list)) {
			list.forEach(menu -> {
				MenuVo menuVo = BeanUtil.copyProperties(menu,MenuVo.class);
				menuList.add(menuVo);
			});
		}
		return menuList;
	}

	@Override
	public List<Tree<Long>> buildMenuTreeSelect(List<MenuVo> menus) {
		if (CollUtil.isEmpty(menus)) {
			return CollUtil.newArrayList();
		}
		return TreeBuildUtils.build(menus, (menu, tree) ->
				tree.setId(menu.getId())
						.setParentId(menu.getParentId())
						.setName(menu.getMenuName())
						.setWeight(menu.getOrderNum())
						.putExtra("path", menu.getPath()));
	}
}
